<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :before-close="closeDailog"
        destroy-on-close :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="语录内容" prop="content">
                <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="语录内容"
                    v-model="dataForm.content">
                </el-input>
            </el-form-item>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="语录类型" prop="type">
                        <ec-dict-select dictType="inspquo_type" v-model="dataForm.type" placeholder="语录类型">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="删除标识" prop="isDelete">
                        <ec-dict-select dictType="is_delete" v-model="dataForm.isDelete" placeholder="删除标识">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="指定日期" prop="scheduledDate">
                        <el-date-picker v-model="dataForm.scheduledDate" placeholder="指定日期" style="width:100%">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属部门" prop="pkDept">
                        <ec-dept-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" :pkOrg="dataForm.pkOrg"
                            v-model="dataForm.pkDept" placeholder="请选择所属部门">
                        </ec-dept-tree-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="所属集团" prop="pkGroup">
                        <ec-group-tree-select :visible="visible" v-model="dataForm.pkGroup" placeholder="请选择所属集团">
                        </ec-group-tree-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属组织" prop="pkOrg">
                        <ec-org-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg"
                            placeholder="请选择所属组织"></ec-org-tree-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as inspquoService from '@/api/bdms/inspquo'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    id: 0,
                    content: '',
                    type: '1',
                    scheduledDate: '',
                    pkGroup: '',
                    pkOrg: '',
                    pkDept: '',
                    isDelete: 'N',
                    revision: '',
                },
                dataRule: {
                    content: [{
                        required: true,
                        message: '语录内容不能为空',
                        trigger: 'blur'
                    }],
                    type: [{
                        required: true,
                        message: '语录类型不能为空',
                        trigger: 'blur'
                    }],
                    scheduledDate: [{
                        required: false,
                        message: '指定日期不能为空',
                        trigger: 'blur'
                    }],
                    pkGroup: [{
                        required: false,
                        message: '所属集团不能为空',
                        trigger: 'blur'
                    }],
                    pkOrg: [{
                        required: false,
                        message: '所属组织不能为空',
                        trigger: 'blur'
                    }],
                    pkDept: [{
                        required: false,
                        message: '所属部门不能为空',
                        trigger: 'blur'
                    }],
                    isDelete: [{
                        required: false,
                        message: '删除标识不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        inspquoService.getinspquoinfo(this.dataForm.id).then(data => {
                            this.dataForm.content = data.content
                            this.dataForm.type = data.type
                            this.dataForm.scheduledDate = data.scheduledDate
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.pkDept = data.pkDept
                            this.dataForm.isDelete = data.isDelete
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'id': this.dataForm.id || undefined,
                            'content': this.dataForm.content,
                            'type': this.dataForm.type,
                            'scheduledDate': this.dataForm.scheduledDate,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'pkDept': this.dataForm.pkDept,
                            'isDelete': this.dataForm.isDelete,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.id ? inspquoService.addinspquo(params) : inspquoService
                            .updateinspquo(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>